package com.baomidou.mybatisplus.core.conditions.segments;

import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.enums.WrapperKeyword;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>MatchSegment</p>
 *
 * <p>项目名称：fakerMybatisPlusUtil</p>
 *
 * <p>注释:无</p>
 *
 * <p>Copyright: Copyright Faker(c) 2023/1/20</p>
 *
 * <p>公司: Faker</p>
 *
 * @author 淡梦如烟
 * @version 1.0
 * @date 2023/1/20 21:57
 */
public enum MatchSegment {
    GROUP_BY(SqlKeyword.GROUP_BY),
    ORDER_BY(SqlKeyword.ORDER_BY),
    NOT(SqlKeyword.NOT),
    AND(SqlKeyword.AND),
    OR(SqlKeyword.OR),
    AND_OR(SqlKeyword.AND, SqlKeyword.OR),
    EXISTS(SqlKeyword.EXISTS),
    HAVING(SqlKeyword.HAVING),
    APPLY(WrapperKeyword.APPLY),
    BRACKET(WrapperKeyword.BRACKET);

    private List<ISqlSegment> predicate = new ArrayList<ISqlSegment>();

    MatchSegment(ISqlSegment predicate) {
        this.predicate.add(predicate);
    }


    MatchSegment(SqlKeyword and, SqlKeyword or) {
        this.predicate.add(and);
        this.predicate.add(or);
    }

    /**
     * 重写匹配方法
     *
     * @param segment
     * @return
     */
    public boolean match(ISqlSegment segment) {
        for (ISqlSegment isqlSegment : this.predicate) {
//            if (isqlSegment != null && segment != null) {
//                System.out.println(isqlSegment.getSqlSegment() + " == " + segment.getSqlSegment());
//            }
            if (isqlSegment != null && isqlSegment.getSqlSegment() != null && segment != null && isqlSegment.getSqlSegment().equals(segment.getSqlSegment())) {
                return true;
            }
        }
        return false;
    }

}
